package de.ret_world.openrpgstudio.editor;

import de.ret_world.openrpgstudio.editor.script.ScriptObject;
import java.util.HashMap;

/**
 * The Database stores all your ScriptObject data. You can read all information through a Lua library and use it in your scripts.
 *
 * @author emu
 */
public class Database {

    private HashMap<String, HashMap<String, ScriptObject>> database;

    protected Database() {
        database = new HashMap<>();
    }

    /**
     * Puts your created ScriptObject in the database. The 'type' is just an identifier for your kind of ScriptObject (e.g. the class name of your ScriptObject).
     * @param type identifier for the kind of ScriptObject
     * @param id the id of the ScriptObject
     * @param so the ScriptObject
     */
    public void putScriptObject(String type, String id, ScriptObject so) {
        HashMap<String, ScriptObject> m = database.get(type);
        if (m == null) {
            m = new HashMap<>();
            database.put(type, m);
        }
        m.put(id, so);
    }

    /**
     * Gets a stored ScriptObject. The 'type' is just an identifier for your kind of ScriptObject (e.g. the class name of your ScriptObject).
     * @param type identifier for the kind of ScriptObject
     * @param id the id of the ScriptObject
     * @return the stored ScriptObject
     */
    public ScriptObject getScriptObject(String type, String id) {
        HashMap<String, ScriptObject> m = database.get(type);
        if (m == null) {
            return null;
        }
        return m.get(id);
    }

    /**
     * Removes the stored ScriptObject bound to the given key.
     * @param type identifier for the kind of ScriptObject
     * @param id the key of the ScriptObject
     */
    public void removeScriptObject(String type, String id) {
        HashMap<String, ScriptObject> m = database.get(type);
        if (m != null) {
            m.remove(id);
        }
    }

    /**
     * Gets a HashMap of ScriptObjects (bound to their ID) of the given type.
     * @param type identifier for the kind of ScriptObjects
     * @return a HashMap of ScriptObjects
     */
    public HashMap<String, ScriptObject> getScriptMap(String type) {
        return database.get(type);
    }

    /**
     * Deletes every stored ScriptObject
     */
    public void clear() {
        database = new HashMap<>();
    }
}
